package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Respuestaxasignatura;
import com.lostreseditores.sabersai.model.dto.RespuestaxasignaturaDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class RespuestaxasignaturaView {
    private InputText txtRespxasigactivon;
    private InputText txtRespxasigcorrecv;
    private InputText txtRespxasigdifn;
    private InputText txtRespxasignivdesv;
    private InputText txtRespxasigniveldesv;
    private InputText txtRespxasigopan;
    private InputText txtRespxasigopbn;
    private InputText txtRespxasigopcn;
    private InputText txtRespxasigopdn;
    private InputText txtRespxasigordenn;
    private InputText txtRespxasigpregespn;
    private InputText txtRespxasigtxtn;
    private InputText txtAsigxexamidn_Asignaturaxexamen;
    private InputText txtComciadn_Competencia;
    private InputText txtCompciaidn_Componente;
    private InputText txtRespxasigidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<RespuestaxasignaturaDTO> data;
    private RespuestaxasignaturaDTO selectedRespuestaxasignatura;

    public RespuestaxasignaturaView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            RespuestaxasignaturaDTO respuestaxasignaturaDTO = (RespuestaxasignaturaDTO) e.getObject();

            if (txtRespxasigactivon == null) {
                txtRespxasigactivon = new InputText();
            }

            txtRespxasigactivon.setValue(respuestaxasignaturaDTO.getRespxasigactivon());

            if (txtRespxasigcorrecv == null) {
                txtRespxasigcorrecv = new InputText();
            }

            txtRespxasigcorrecv.setValue(respuestaxasignaturaDTO.getRespxasigcorrecv());

            if (txtRespxasigdifn == null) {
                txtRespxasigdifn = new InputText();
            }

            txtRespxasigdifn.setValue(respuestaxasignaturaDTO.getRespxasigdifn());

            if (txtRespxasignivdesv == null) {
                txtRespxasignivdesv = new InputText();
            }

            txtRespxasignivdesv.setValue(respuestaxasignaturaDTO.getRespxasignivdesv());

            if (txtRespxasigniveldesv == null) {
                txtRespxasigniveldesv = new InputText();
            }

            txtRespxasigniveldesv.setValue(respuestaxasignaturaDTO.getRespxasigniveldesv());

            if (txtRespxasigopan == null) {
                txtRespxasigopan = new InputText();
            }

            txtRespxasigopan.setValue(respuestaxasignaturaDTO.getRespxasigopan());

            if (txtRespxasigopbn == null) {
                txtRespxasigopbn = new InputText();
            }

            txtRespxasigopbn.setValue(respuestaxasignaturaDTO.getRespxasigopbn());

            if (txtRespxasigopcn == null) {
                txtRespxasigopcn = new InputText();
            }

            txtRespxasigopcn.setValue(respuestaxasignaturaDTO.getRespxasigopcn());

            if (txtRespxasigopdn == null) {
                txtRespxasigopdn = new InputText();
            }

            txtRespxasigopdn.setValue(respuestaxasignaturaDTO.getRespxasigopdn());

            if (txtRespxasigordenn == null) {
                txtRespxasigordenn = new InputText();
            }

            txtRespxasigordenn.setValue(respuestaxasignaturaDTO.getRespxasigordenn());

            if (txtRespxasigpregespn == null) {
                txtRespxasigpregespn = new InputText();
            }

            txtRespxasigpregespn.setValue(respuestaxasignaturaDTO.getRespxasigpregespn());

            if (txtRespxasigtxtn == null) {
                txtRespxasigtxtn = new InputText();
            }

            txtRespxasigtxtn.setValue(respuestaxasignaturaDTO.getRespxasigtxtn());

            if (txtAsigxexamidn_Asignaturaxexamen == null) {
                txtAsigxexamidn_Asignaturaxexamen = new InputText();
            }

            txtAsigxexamidn_Asignaturaxexamen.setValue(respuestaxasignaturaDTO.getAsigxexamidn_Asignaturaxexamen());

            if (txtComciadn_Competencia == null) {
                txtComciadn_Competencia = new InputText();
            }

            txtComciadn_Competencia.setValue(respuestaxasignaturaDTO.getComciadn_Competencia());

            if (txtCompciaidn_Componente == null) {
                txtCompciaidn_Componente = new InputText();
            }

            txtCompciaidn_Componente.setValue(respuestaxasignaturaDTO.getCompciaidn_Componente());

            if (txtRespxasigidn == null) {
                txtRespxasigidn = new InputText();
            }

            txtRespxasigidn.setValue(respuestaxasignaturaDTO.getRespxasigidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtRespxasigactivon != null) {
            txtRespxasigactivon.setValue(null);
            txtRespxasigactivon.setDisabled(true);
        }

        if (txtRespxasigcorrecv != null) {
            txtRespxasigcorrecv.setValue(null);
            txtRespxasigcorrecv.setDisabled(true);
        }

        if (txtRespxasigdifn != null) {
            txtRespxasigdifn.setValue(null);
            txtRespxasigdifn.setDisabled(true);
        }

        if (txtRespxasignivdesv != null) {
            txtRespxasignivdesv.setValue(null);
            txtRespxasignivdesv.setDisabled(true);
        }

        if (txtRespxasigniveldesv != null) {
            txtRespxasigniveldesv.setValue(null);
            txtRespxasigniveldesv.setDisabled(true);
        }

        if (txtRespxasigopan != null) {
            txtRespxasigopan.setValue(null);
            txtRespxasigopan.setDisabled(true);
        }

        if (txtRespxasigopbn != null) {
            txtRespxasigopbn.setValue(null);
            txtRespxasigopbn.setDisabled(true);
        }

        if (txtRespxasigopcn != null) {
            txtRespxasigopcn.setValue(null);
            txtRespxasigopcn.setDisabled(true);
        }

        if (txtRespxasigopdn != null) {
            txtRespxasigopdn.setValue(null);
            txtRespxasigopdn.setDisabled(true);
        }

        if (txtRespxasigordenn != null) {
            txtRespxasigordenn.setValue(null);
            txtRespxasigordenn.setDisabled(true);
        }

        if (txtRespxasigpregespn != null) {
            txtRespxasigpregespn.setValue(null);
            txtRespxasigpregespn.setDisabled(true);
        }

        if (txtRespxasigtxtn != null) {
            txtRespxasigtxtn.setValue(null);
            txtRespxasigtxtn.setDisabled(true);
        }

        if (txtAsigxexamidn_Asignaturaxexamen != null) {
            txtAsigxexamidn_Asignaturaxexamen.setValue(null);
            txtAsigxexamidn_Asignaturaxexamen.setDisabled(true);
        }

        if (txtComciadn_Competencia != null) {
            txtComciadn_Competencia.setValue(null);
            txtComciadn_Competencia.setDisabled(true);
        }

        if (txtCompciaidn_Componente != null) {
            txtCompciaidn_Componente.setValue(null);
            txtCompciaidn_Componente.setDisabled(true);
        }

        if (txtRespxasigidn != null) {
            txtRespxasigidn.setValue(null);
            txtRespxasigidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Respuestaxasignatura entity = null;

        try {
            Integer respxasigidn = new Integer(txtRespxasigidn.getValue()
                                                              .toString());
            entity = BusinessDelegatorView.getRespuestaxasignatura(respxasigidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtRespxasigactivon.setDisabled(false);
            txtRespxasigcorrecv.setDisabled(false);
            txtRespxasigdifn.setDisabled(false);
            txtRespxasignivdesv.setDisabled(false);
            txtRespxasigniveldesv.setDisabled(false);
            txtRespxasigopan.setDisabled(false);
            txtRespxasigopbn.setDisabled(false);
            txtRespxasigopcn.setDisabled(false);
            txtRespxasigopdn.setDisabled(false);
            txtRespxasigordenn.setDisabled(false);
            txtRespxasigpregespn.setDisabled(false);
            txtRespxasigtxtn.setDisabled(false);
            txtAsigxexamidn_Asignaturaxexamen.setDisabled(false);
            txtComciadn_Competencia.setDisabled(false);
            txtCompciaidn_Componente.setDisabled(false);
            txtRespxasigidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtRespxasigactivon.setValue(entity.getRespxasigactivon());
            txtRespxasigactivon.setDisabled(false);
            txtRespxasigcorrecv.setValue(entity.getRespxasigcorrecv());
            txtRespxasigcorrecv.setDisabled(false);
            txtRespxasigdifn.setValue(entity.getRespxasigdifn());
            txtRespxasigdifn.setDisabled(false);
            txtRespxasignivdesv.setValue(entity.getRespxasignivdesv());
            txtRespxasignivdesv.setDisabled(false);
            txtRespxasigniveldesv.setValue(entity.getRespxasigniveldesv());
            txtRespxasigniveldesv.setDisabled(false);
            txtRespxasigopan.setValue(entity.getRespxasigopan());
            txtRespxasigopan.setDisabled(false);
            txtRespxasigopbn.setValue(entity.getRespxasigopbn());
            txtRespxasigopbn.setDisabled(false);
            txtRespxasigopcn.setValue(entity.getRespxasigopcn());
            txtRespxasigopcn.setDisabled(false);
            txtRespxasigopdn.setValue(entity.getRespxasigopdn());
            txtRespxasigopdn.setDisabled(false);
            txtRespxasigordenn.setValue(entity.getRespxasigordenn());
            txtRespxasigordenn.setDisabled(false);
            txtRespxasigpregespn.setValue(entity.getRespxasigpregespn());
            txtRespxasigpregespn.setDisabled(false);
            txtRespxasigtxtn.setValue(entity.getRespxasigtxtn());
            txtRespxasigtxtn.setDisabled(false);
            txtAsigxexamidn_Asignaturaxexamen.setValue(entity.getAsignaturaxexamen()
                                                             .getAsigxexamidn());
            txtAsigxexamidn_Asignaturaxexamen.setDisabled(false);
            txtComciadn_Competencia.setValue(entity.getCompetencia()
                                                   .getComciadn());
            txtComciadn_Competencia.setDisabled(false);
            txtCompciaidn_Componente.setValue(entity.getComponente()
                                                    .getCompciaidn());
            txtCompciaidn_Componente.setDisabled(false);
            txtRespxasigidn.setValue(entity.getRespxasigidn());
            txtRespxasigidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveRespuestaxasignatura(FacesUtils.checkInteger(
                    txtRespxasigactivon),
                FacesUtils.checkString(txtRespxasigcorrecv),
                FacesUtils.checkInteger(txtRespxasigdifn),
                FacesUtils.checkInteger(txtRespxasigidn),
                FacesUtils.checkString(txtRespxasignivdesv),
                FacesUtils.checkString(txtRespxasigniveldesv),
                FacesUtils.checkInteger(txtRespxasigopan),
                FacesUtils.checkInteger(txtRespxasigopbn),
                FacesUtils.checkInteger(txtRespxasigopcn),
                FacesUtils.checkInteger(txtRespxasigopdn),
                FacesUtils.checkInteger(txtRespxasigordenn),
                FacesUtils.checkInteger(txtRespxasigpregespn),
                FacesUtils.checkInteger(txtRespxasigtxtn),
                FacesUtils.checkInteger(txtAsigxexamidn_Asignaturaxexamen),
                FacesUtils.checkInteger(txtComciadn_Competencia),
                FacesUtils.checkInteger(txtCompciaidn_Componente));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteRespuestaxasignatura(FacesUtils.checkInteger(
                    txtRespxasigidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateRespuestaxasignatura(FacesUtils.checkInteger(
                    txtRespxasigactivon),
                FacesUtils.checkString(txtRespxasigcorrecv),
                FacesUtils.checkInteger(txtRespxasigdifn),
                FacesUtils.checkInteger(txtRespxasigidn),
                FacesUtils.checkString(txtRespxasignivdesv),
                FacesUtils.checkString(txtRespxasigniveldesv),
                FacesUtils.checkInteger(txtRespxasigopan),
                FacesUtils.checkInteger(txtRespxasigopbn),
                FacesUtils.checkInteger(txtRespxasigopcn),
                FacesUtils.checkInteger(txtRespxasigopdn),
                FacesUtils.checkInteger(txtRespxasigordenn),
                FacesUtils.checkInteger(txtRespxasigpregespn),
                FacesUtils.checkInteger(txtRespxasigtxtn),
                FacesUtils.checkInteger(txtAsigxexamidn_Asignaturaxexamen),
                FacesUtils.checkInteger(txtComciadn_Competencia),
                FacesUtils.checkInteger(txtCompciaidn_Componente));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtRespxasigidn == null) {
                txtRespxasigidn = new InputText();
            }

            txtRespxasigidn.setValue(selectedRespuestaxasignatura.getRespxasigidn());

            BusinessDelegatorView.deleteRespuestaxasignatura(FacesUtils.checkInteger(
                    txtRespxasigidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedRespuestaxasignatura);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer respxasigactivon,
        String respxasigcorrecv, Integer respxasigdifn, Integer respxasigidn,
        String respxasignivdesv, String respxasigniveldesv,
        Integer respxasigopan, Integer respxasigopbn, Integer respxasigopcn,
        Integer respxasigopdn, Integer respxasigordenn,
        Integer respxasigpregespn, Integer respxasigtxtn,
        Integer asigxexamidn_Asignaturaxexamen, Integer comciadn_Competencia,
        Integer compciaidn_Componente) throws Exception {
        try {
            BusinessDelegatorView.updateRespuestaxasignatura(respxasigactivon,
                respxasigcorrecv, respxasigdifn, respxasigidn,
                respxasignivdesv, respxasigniveldesv, respxasigopan,
                respxasigopbn, respxasigopcn, respxasigopdn, respxasigordenn,
                respxasigpregespn, respxasigtxtn,
                asigxexamidn_Asignaturaxexamen, comciadn_Competencia,
                compciaidn_Componente);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("RespuestaxasignaturaView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtRespxasigactivon() {
        return txtRespxasigactivon;
    }

    public void setTxtRespxasigactivon(InputText txtRespxasigactivon) {
        this.txtRespxasigactivon = txtRespxasigactivon;
    }

    public InputText getTxtRespxasigcorrecv() {
        return txtRespxasigcorrecv;
    }

    public void setTxtRespxasigcorrecv(InputText txtRespxasigcorrecv) {
        this.txtRespxasigcorrecv = txtRespxasigcorrecv;
    }

    public InputText getTxtRespxasigdifn() {
        return txtRespxasigdifn;
    }

    public void setTxtRespxasigdifn(InputText txtRespxasigdifn) {
        this.txtRespxasigdifn = txtRespxasigdifn;
    }

    public InputText getTxtRespxasignivdesv() {
        return txtRespxasignivdesv;
    }

    public void setTxtRespxasignivdesv(InputText txtRespxasignivdesv) {
        this.txtRespxasignivdesv = txtRespxasignivdesv;
    }

    public InputText getTxtRespxasigniveldesv() {
        return txtRespxasigniveldesv;
    }

    public void setTxtRespxasigniveldesv(InputText txtRespxasigniveldesv) {
        this.txtRespxasigniveldesv = txtRespxasigniveldesv;
    }

    public InputText getTxtRespxasigopan() {
        return txtRespxasigopan;
    }

    public void setTxtRespxasigopan(InputText txtRespxasigopan) {
        this.txtRespxasigopan = txtRespxasigopan;
    }

    public InputText getTxtRespxasigopbn() {
        return txtRespxasigopbn;
    }

    public void setTxtRespxasigopbn(InputText txtRespxasigopbn) {
        this.txtRespxasigopbn = txtRespxasigopbn;
    }

    public InputText getTxtRespxasigopcn() {
        return txtRespxasigopcn;
    }

    public void setTxtRespxasigopcn(InputText txtRespxasigopcn) {
        this.txtRespxasigopcn = txtRespxasigopcn;
    }

    public InputText getTxtRespxasigopdn() {
        return txtRespxasigopdn;
    }

    public void setTxtRespxasigopdn(InputText txtRespxasigopdn) {
        this.txtRespxasigopdn = txtRespxasigopdn;
    }

    public InputText getTxtRespxasigordenn() {
        return txtRespxasigordenn;
    }

    public void setTxtRespxasigordenn(InputText txtRespxasigordenn) {
        this.txtRespxasigordenn = txtRespxasigordenn;
    }

    public InputText getTxtRespxasigpregespn() {
        return txtRespxasigpregespn;
    }

    public void setTxtRespxasigpregespn(InputText txtRespxasigpregespn) {
        this.txtRespxasigpregespn = txtRespxasigpregespn;
    }

    public InputText getTxtRespxasigtxtn() {
        return txtRespxasigtxtn;
    }

    public void setTxtRespxasigtxtn(InputText txtRespxasigtxtn) {
        this.txtRespxasigtxtn = txtRespxasigtxtn;
    }

    public InputText getTxtAsigxexamidn_Asignaturaxexamen() {
        return txtAsigxexamidn_Asignaturaxexamen;
    }

    public void setTxtAsigxexamidn_Asignaturaxexamen(
        InputText txtAsigxexamidn_Asignaturaxexamen) {
        this.txtAsigxexamidn_Asignaturaxexamen = txtAsigxexamidn_Asignaturaxexamen;
    }

    public InputText getTxtComciadn_Competencia() {
        return txtComciadn_Competencia;
    }

    public void setTxtComciadn_Competencia(InputText txtComciadn_Competencia) {
        this.txtComciadn_Competencia = txtComciadn_Competencia;
    }

    public InputText getTxtCompciaidn_Componente() {
        return txtCompciaidn_Componente;
    }

    public void setTxtCompciaidn_Componente(InputText txtCompciaidn_Componente) {
        this.txtCompciaidn_Componente = txtCompciaidn_Componente;
    }

    public InputText getTxtRespxasigidn() {
        return txtRespxasigidn;
    }

    public void setTxtRespxasigidn(InputText txtRespxasigidn) {
        this.txtRespxasigidn = txtRespxasigidn;
    }

    public List<RespuestaxasignaturaDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataRespuestaxasignatura();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<RespuestaxasignaturaDTO> respuestaxasignaturaDTO) {
        this.data = respuestaxasignaturaDTO;
    }

    public RespuestaxasignaturaDTO getSelectedRespuestaxasignatura() {
        return selectedRespuestaxasignatura;
    }

    public void setSelectedRespuestaxasignatura(
        RespuestaxasignaturaDTO respuestaxasignatura) {
        this.selectedRespuestaxasignatura = respuestaxasignatura;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
